﻿using Common.Model;
using Common.Service;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows.Controls;

namespace HPMTool.LeftNavigation.ViewModels
{
    public class LeftNavigationViewModel : BindableBase
    {


        public static LeftNavigationViewModel Instance;

        private string _message;
        public string Message
        {
            get => _message;
            set => SetProperty(ref _message, value);
        }


        private ObservableCollection<ModuleModel> _dataService = new ObservableCollection<ModuleModel>();
        public ObservableCollection<ModuleModel> DataService
        {
            get => _dataService;
            set => SetProperty(ref _dataService, value);
        }


        public DelegateCommand<SelectionChangedEventArgs> SelectedCommand { get; private set; }

        private ObservableCollection<ModuleModel> moduleCollection = null;
        public IRegionManager _regionManager;
        private IModuleCatalog _moduleCatalog;

        public LeftNavigationViewModel(IRegionManager regionManager,
                                        IModuleCatalog moduleCatalog)

        {


            _regionManager = regionManager;
            _moduleCatalog = moduleCatalog;

            SelectedCommand = new DelegateCommand<SelectionChangedEventArgs>(OnItemSelected);


            #region 获取所有模块信息
            DirectoryModuleCatalog directoryCatalog = (DirectoryModuleCatalog)_moduleCatalog;
            directoryCatalog.Initialize();

            moduleCollection = new ObservableCollection<ModuleModel>();
            TypeFilter typeFilter = new TypeFilter(InterfaceFilter);

            foreach (IModuleCatalogItem item in directoryCatalog.Items)
            {
                ModuleInfo moduleInfo = (ModuleInfo)item;

                // in .NetFrameWork we dont need to replace
                Assembly asm = Assembly.LoadFrom(moduleInfo.Ref.Replace(@"file:///", ""));

                foreach (Type t in asm.GetTypes())
                {
                    Type[] myInterfaces = t.FindInterfaces(typeFilter, typeof(IModuleService).ToString());

                    if (myInterfaces.Length > 0)
                    {
                        IModuleService moduleService = (IModuleService)asm.CreateInstance(t.FullName);
                        ModuleModel module = moduleService.GetModule();
                        moduleCollection.Add(module);
                    }
                }
            }

            Instance = this;
            Instance.DataService.AddRange(moduleCollection);

            #endregion
        }


        private bool InterfaceFilter(Type typeObj, object criteriaObj)
        {
            return typeObj.ToString() == criteriaObj.ToString();
        }


        private void OnItemSelected(SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
            {
                return;
            }

            if (e.AddedItems[0] is ModuleModel item)
            {

                //Clear last module
                foreach (object view in _regionManager.Regions["MainWindow.ModuleContentRegion"].Views)
                {
                    _regionManager.Regions["MainWindow.ModuleContentRegion"].Remove(view);
                }

                // register module
                _regionManager.RegisterViewWithRegion("MainWindow.ModuleContentRegion", item.DefaultView);
            }
        }



    }
}
